package com.nation.controller;

import com.nation.domain.TNatureAndResource;
import com.nation.domain.User;
import com.nation.domain.VEconomicAndDevelopment;
import com.nation.service.NatureService;
import com.nation.utils.ReadAndStoreFile;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/3.
 */
@Controller
@RequestMapping(value="Nature")
public class NatureController {
    @Autowired
    NatureService NatureService;

    /**
     * 自然环境与资源
     * @param map
     * @return
     */
    @RequestMapping(value = "/nature")
    public String environment(ModelMap map){
        List<TNatureAndResource> nlist=NatureService.findAllNature();
        map.put("nlist",nlist);
        return "Nature/nature";
    }


    @RequestMapping(value = "/nature_detail")
    public String nature_detail(String country_code,ModelMap map){
        TNatureAndResource nature=NatureService.findByCountryCode(country_code);
        map.put("nature",nature);
        return "Nature/nature-detail";
    }

    /**
     * 修改界面
     * @param country_code
     * @param map
     * @return
     */
    @RequestMapping(value = "/nature_update")
    public String nature_update(String country_code,ModelMap map){
        TNatureAndResource nature=NatureService.findByCountryCode(country_code);
        map.put("nature",nature);
        return "Nature/nature-update";
    }

    /**
     * 修改信息
     * @param nature
     * @return
     */
    @RequestMapping(value = "/nature_update.do")
    @ResponseBody
    public String nature_update_do(TNatureAndResource nature){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        nature.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=NatureService.updateNature(nature);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }

    @RequestMapping(value = "/nature_add")
    public String nature_add(){
        return "Nature/nature-add";
    }
    @RequestMapping(value = "/nature_add.do")
    @ResponseBody
    public String nature_add_do(HttpServletRequest request,String statistics,String conferences,String geography,String climate,
                                String mineral_resource,String biological_resource,String energy_resource,String country_code,String reference,
                                @RequestParam(value = "picture",required = false)MultipartFile[] pictures){

        TNatureAndResource TNatureAndResource=new TNatureAndResource(statistics,conferences,geography,climate,mineral_resource,biological_resource,energy_resource,country_code,reference);
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        TNatureAndResource.setRecoder_id(user.getYHZH());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        TNatureAndResource.setRecoder_time(sdf.format(System.currentTimeMillis()));
        Boolean judge=null;
        try{
            if (pictures!=null&&pictures.length>0) {//有文件上传
                String path=request.getSession().getServletContext().getResource("/").getPath();
                path += ReadAndStoreFile.NATURE;
                System.out.println("path--"+path);

                String picture_path="";
//          将MultipartFile格式转换成File
                for (int i=0;i<pictures.length;i++){
                    String temp=ReadAndStoreFile.save(pictures[i],path);
                    temp=temp.substring(temp.indexOf("nation/"+ReadAndStoreFile.NATURE));
                    if (i == (pictures.length-1) || pictures.length == 1){
                        picture_path=temp+picture_path;
                    }else{
                        picture_path+=temp+",";
                    }
                }
                TNatureAndResource.setPicture(picture_path);
            }

            judge=NatureService.insert(TNatureAndResource);
        }catch (Exception e){
            e.printStackTrace();
            return "添加失败";
        }
        if(judge)
            return "添加成功";
        else
            return "添加失败";
    }
    @RequestMapping(value = "/InfoSearch")
    public String InfoSearch(HttpServletRequest request,ModelMap mmap){
        String recoder_id=request.getParameter("recoder_id");
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("recoder_id",recoder_id);
        List<TNatureAndResource> elist=NatureService.findByCondition(map);
        mmap.put("nlist",elist);
        return "Nature/nature";
    }
    @RequestMapping(value = "/nature_delete")
    @ResponseBody
    public String nature_delete(String country_code){
        Boolean judge=null;
        try{
            judge=NatureService.delete(country_code);
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
        if(judge)
            return "删除成功";
        else
            return "删除失败";
    }
}
